<template>
	<view class="recommend">
		<view class="section_title">
			<text class="title">{{ section.title }}</text>
			<text class="more" @click="$emit('more')" v-if="section.hasmore">更多></text>
		</view>

		<!-- 网格布局 -->
		<view class="album_grid" v-if="section.showtype === 'grid'">
			<view class="album_item" v-for="(item, idx) in section.items" :key="idx" @click="$emit('detail', item)">
				<view class="image-wrapper">
					<image :src="item.coverUrl" mode="aspectFill" />
					<view v-if="section.showViewCountType === 'onimg'" class="album_view overlay">
						<u-icon name="play-right" size="12" color="#fff" />
						<text>{{ formatViewCount(item.viewCount) }}</text>
					</view>
				</view>
				<view class="album-info">
					<text class="album_name">{{ item.name }}</text>
					<text class="album_desc u-line-2">{{ item.description }}</text>
					<view v-if="section.showViewCountType === 'onfoot'" class="album_view">
						<u-icon name="play-right-fill" size="12" />
						<text>{{ formatViewCount(item.viewCount) }}</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 横向滚动布局 -->
		<scroll-view class="album_scroll" scroll-x v-if="section.showtype === 'scroll'">
			<view class="album_scroll_content">
				<view class="album_scroll_item" v-for="(item, idx) in section.items" :key="idx" @click="$emit('detail', item)">
					<view class="image-wrapper">
						<image :src="item.coverUrl" mode="aspectFill" />
						<view v-if="section.showViewCountType === 'onimg'" class="album_view overlay">
							<u-icon name="play-right" size="12" color="#fff" />
							<text>{{ formatViewCount(item.viewCount) }}</text>
						</view>
					</view>
					<view class="album-info">
						<view class="album_name">{{ item.name }}</view>
						<view class="album_desc u-line-2">{{ item.description }}</view>

						<view v-if="section.showViewCountType === 'onfoot'" class="album_view">
							<u-icon name="play-right-fill" size="12" />
							<text>{{ formatViewCount(item.viewCount) }}</text>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>

		<view class="album_swiper" v-if="section.showtype === 'swiper'">
			<swiper :style="{ height: 180 * section.showViewCount + 'rpx' }" next-margin="40" class="album_swiper_bar">
				<swiper-item v-for="(swiperItem, index) in Math.ceil(section.items.length / section.showViewCount)" :key="index">
					<view
						class="album_swiper_item"
						v-for="(item, idx) in section.items.slice(index, index + section.showViewCount)"
						:key="idx"
						@click="$emit('detail', item)"
					>
						<view class="image-wrapper">
							<image :src="item.coverUrl" mode="aspectFill" />
						</view>
						<view class="album-info">
							<view class="album_name">{{ item.name }}</view>
							<view class="album_desc u-line-2">{{ item.description }}</view>

							<view class="album_view">
								<u-icon name="play-right-fill" size="12" />
								<text>{{ formatViewCount(item.viewCount) }}</text>
							</view>
						</view>
					</view>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>

<script>
import { formatViewCount } from '../utils/format.js';

export default {
	props: {
		section: {
			type: Object,
			required: true
		}
	},
	methods: {
		formatViewCount
	}
};
</script>
<style lang="scss" scoped>
.recommend {
	margin-top: 20rpx;
	background: #fff;
	padding: 20rpx;

	.section_title {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;

		.title {
			font-size: 32rpx;
			font-weight: bold;
		}
		.more {
			font-size: 24rpx;
			color: #666;
		}
	}

	// 网格布局样式
	.album_grid {
		display: flex;
		flex-wrap: wrap;
		margin: 0 -10rpx;

		.album_item {
			width: calc(33.33% - 20rpx);
			margin: 10rpx;
		}
	}

	// 横向滚动布局样式
	.album_scroll {
		width: 100%;
		white-space: nowrap;

		.album_scroll_content {
			display: inline-block;
			padding: 10rpx 0;
		}

		.album_scroll_item {
			display: inline-block;
			width: 200rpx;
			margin-right: 15rpx;

			&:last-child {
				margin-right: 30rpx;
			}
		}
	}
}

.image-wrapper {
	position: relative;
	width: 100%;
	display: block;
	font-size: 0;

	image {
		width: 100%;
		height: 240rpx;
		border-radius: 8rpx;
		display: block;
	}

	.album_view.overlay {
		box-sizing: border-box;
		position: absolute;
		bottom: 0;
		left: 0;
		right: 0;
		background: linear-gradient(to top, rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0));
		color: #fff;
		padding: 8rpx;
		font-size: 20rpx;
		display: flex;
		align-items: center;
		justify-content: flex-start;
		border-radius: 0 0 8rpx 8rpx;
		white-space: nowrap;
		overflow: hidden;
		height: 60rpx;

		.u-icon {
			flex-shrink: 0;
			margin-right: 4rpx;
			display: inline-flex;
		}
	}
}

// 横向滚动布局中的图片尺寸调整
.album_scroll {
	.album_scroll_item {
		.image-wrapper {
			image {
				width: 200rpx;
				height: 200rpx;
			}
		}
	}
}

.album-info {
	.album_name {
		font-size: 32rpx;
		font-weight: bold;
		margin-top: 10rpx;
		display: block;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.album_desc {
		font-size: 24rpx;
		color: #666;
		display: block;
		margin-top: 6rpx;
		max-width: 400rpx;
		// white-space: nowrap;
		// overflow: hidden;
		// text-overflow: ellipsis;
	}

	.album_view {
		font-size: 24rpx;
		color: #666;
		display: flex;
		gap: 10rpx;
		align-items: center;
		margin-top: 6rpx;
		white-space: nowrap;
		overflow: hidden;

		.u-icon {
			flex-shrink: 0;
			margin-right: 4rpx;
			display: inline-flex;
		}
	}
}

.album_swiper {
	.album_swiper_bar {
		.album_swiper_item {
			display: flex;
			gap: 40rpx;
			margin-bottom: 30rpx;
			.image-wrapper {
				width: 150rpx;
				image {
					width: 150rpx;
					height: 150rpx;
				}
			}
			.album-info {
				display: flex;
				flex-direction: column;
				justify-content: space-between;
			}
		}
	}
}
</style>
